﻿using ApiManage.Models.RequestModels;
using ApiManage.Models.zyhModels;
using ApiManage.Svc.Filter;
using Microsoft.AspNetCore.Mvc;
using Rhonin.Common.Entity.Api;
using Rhonin.Common.Entity.Exception;
using Rhonin.Common.Entity.Page;
using Rhonin.Common.Extentions;
using System;
using System.Linq;
using System.Linq.Expressions;
using ApiManage.Service.MsSql.IService;
using ApiManage.Service.MsSql.Service;

namespace ApiManage.Svc.Controllers.System
{
    /// <summary>
    /// 消息管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class SysMessageController : BaseController
    {
        private readonly ISysMessageService _sysMessageService = new SysMessageService();
        /// <summary>
        /// 分页获取消息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="type"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [Route("GetPage"), HttpGet, CheckLogin]
        public ApiResult<PageResult<SysMessage>> GetPage(string title, int type, int pageIndex, int pageSize)
        {
            QueryPage param = new QueryPage()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            Expression<Func<SysMessage, bool>> where = o => o.Mark != 9;
            where = where.AndIf(o => o.Title.Contains(title), title.IsNotEmpty());
            where = where.AndIf(o => o.Type == type, type != 0);
            var list = _sysMessageService.GetList(where).ToList();
            var result = new PageResult<SysMessage>()
            {
                TotalCount = list.Count,
                Items = list.OrderByDescending(o => o.IsTop).ThenByDescending(o => o.Createtime).Skip(param.Skip).Take(param.Limit),
                PageParameter = param
            };
            return CreateResult(result);
        }
        /// <summary>
        /// 编辑消息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Save"), HttpPost, CheckLogin]
        public ApiResult UserEdit(SysMessage param)
        {
            if (string.IsNullOrEmpty(param.Title))
                throw new ApiException("标题不能为空");
            if (param.Id == 0)
            {
                _sysMessageService.Insert(param);
            }
            else
            {
                _sysMessageService.Update(param);
            }
            return CreateSuccess();
        }
        /// <summary>
        /// 删除消息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Remove"), HttpPost, CheckLogin]
        public ApiResult UserDel(IdParam param)
        {
            try
            {
                //_SysMessageService.Delete(param.Id);
            }
            catch (Exception ex)
            {
                return CreateError(ex.Message);
            }
            return CreateSuccess();
        }
    }
}